# args.s
.data
.equ SYS_WRITE, 1
.equ SYS_EXIT,  60

.text

enterkey:
    .string "\n"
enterkeylen = .-enterkey

.globl _start

_start:
    mov     %rsp, %rbp
    pop     %rcx                # argc

vnext:
    pop     %rcx                # argv
    test    %rcx, %rcx          # 空指针表明结束
    jz      exit
    mov     %rcx, %rbx
    xor     %rdx, %rdx

strlen:
    movb    (%rbx), %al
    inc     %rdx
    inc     %rbx
    test    %al, %al
    jnz     strlen

    # write(1, argv[%rcx], %rdx)
    mov     %rdx, %rdx          # 参数三：字符串长度
    mov     %rcx, %rsi          # 参数二：要显示的字符串
    mov     $1, %rdi            # 参数一：文件描述符（stdout）
    mov     $SYS_WRITE, %rax    # 系统调用号（sys_write）
    syscall                     # 调用内核功能

    # write(1, "\n", 2)
    mov     $enterkeylen, %rdx
    mov     $enterkey, %rsi
    mov     $1, %rdi
    mov     $SYS_WRITE, %rax
    syscall

    jmp     vnext

exit:
    mov     $SYS_EXIT, %rax     # 系统调用号(sys_exit) 
    mov     $0, %rdi            # 退出代码
    syscall

# to run this file, follow the commands below
# $ as args.s -o args.o
# $ ld args.o -o args
# $ ./args 
# ./args
# $ ./args aaaa
# ./args
# aaaa
